﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.KhaoThi
{
    public partial class UcKiemTraBangDiemThi : QldtSdh.GUI.Common.BaseUserControl, Common.IHocKyChangable
    {
        public DAL.Models.HocKy HocKy { get; private set; }
        bool allowUpdate = false;


        public void SetHocKy(DAL.Models.HocKy newHocKy)
        {
            this.HocKy = newHocKy;
            LoadDotThi();
        }

        public void ChangeHocKy(DAL.Models.HocKy newHocKy)
        {
            // if (newHocKy != this.HocKy)
            this.SetHocKy(newHocKy);
        }

        public UcKiemTraBangDiemThi()
        {
            InitializeComponent();
            gridLop.AddRowIndexing();
            gridHocVien.AddRowIndexing();
        }

        void LoadDotThi()
        {
            allowUpdate = false;
            if (HocKy != null)
            {
                cbxDotThi.DataSource = BizDotThi.GetList(HocKy.MaHocKyGUID);
                cbxDotThi.DisplayMember = "TenDotThi";
                cbxDotThi.ValueMember = "MaDotThiGUID";
            }
            LoadLopHocPhan();
        }

        void LoadLopHocPhan()
        {
            var dotThi = cbxDotThi.SelectedItem as DotThi;
            gridLop.AutoGenerateColumns = false;
            bdLop.DataSource = null;
            if (dotThi != null)
            {
                bdLop.DataSource = BizDotThi_LopHocPhan.GetList(dotThi.MaDotThiGUID);
            }
            gridLop.DataSource = bdLop;
            LoadChiTietLopHocPhan();
        }

        void LoadChiTietLopHocPhan()
        {
            var lop = bdLop.Current as DotThi_LopHocPhan;
            if (lop != null)
            {
                dtNgayThi.Value = lop.NgayThi.HasValue ? lop.NgayThi.Value : DateTime.MinValue;
                txtGioThi.Text = lop.GioThi;
                txtHinhThucThi.Text = lop.HinhThucThi;
                txtPhongThi.Text = lop.MaPhongHoc;
                var lopHocPhan = BizLopHocPhan.GetByID(lop.MaLopHocPhanGUID);
                if (lopHocPhan != null)
                {
                    var hocPhan = BizHocPhan.GetByID(lopHocPhan.MaHocPhanGUID);
                    if (hocPhan != null)
                        txtTenHocPhan.Text = string.Format("[{0}]-{1} ({2} TC)", hocPhan.MaHocPhan, hocPhan.TenHocPhan, hocPhan.SoTinChi);
                    else
                        txtTenHocPhan.Text = "";
                }
                else
                    txtTenHocPhan.Text = "";
            }
            else
            {
                dtNgayThi.Value = dtNgayThi.MinDate;
                txtGioThi.Text = "";
                txtHinhThucThi.Text = "";
                txtPhongThi.Text = "";
                txtTenHocPhan.Text = "";
            }
            LoadDSHOCVIEN();
        }

        void LoadDSHOCVIEN()
        {
            gridHocVien.AutoGenerateColumns = false;
            var dotThi = cbxDotThi.SelectedItem as DotThi;
            var lopHocPhan = bdLop.Current as DotThi_LopHocPhan;
            bdHocVien.DataSource = null;
            if (dotThi != null && lopHocPhan != null)
            {
                var ls = BizLopHocPhan_BangDiem_HocVien.GetListHocVien(dotThi.MaDotThiGUID, lopHocPhan.MaLopHocPhanGUID);
                bdHocVien.DataSource = ls;
                var bangDiem = BizLopHocPhan_BangDiem.GetByID(lopHocPhan.MaLopHocPhanGUID, dotThi.MaDotThiGUID);
                if (bangDiem != null && (bangDiem.TrangThai == (int)TRANGTHAI_BANGDIEM.DANHAPXONG || bangDiem.TrangThai == (int)TRANGTHAI_BANGDIEM.DAPHEDUYET))
                {
                    btnXacNhan.Enabled = btnBaoLoi.Enabled = true;
                }
                else
                    btnXacNhan.Enabled = btnBaoLoi.Enabled = false;
            }
            gridHocVien.DataSource = bdHocVien;
            allowUpdate = true;
        }

        private void bdLop_CurrentChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadChiTietLopHocPhan();
        }

        private void cbxDotThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadLopHocPhan();
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (UpdateBangDiem(TRANGTHAI_BANGDIEM.DAPHEDUYET) == CODE_RESULT_RETURN.ThanhCong)
            {
                DialogUtils.ShowMessage("Đã xác nhận bảng điểm đúng thành công");
                LoadLopHocPhan();
            }
            else
                DialogUtils.ShowError("Lỗi xác nhận bảng điểm");
        }

        private void btnBaoLoi_Click(object sender, EventArgs e)
        {
            if (UpdateBangDiem(TRANGTHAI_BANGDIEM.BAOLOINHAP) == CODE_RESULT_RETURN.ThanhCong)
            {
                DialogUtils.ShowMessage("Đã báo lỗi nhập điểm thành công");
                LoadLopHocPhan();
            }
            else
                DialogUtils.ShowError("Lỗi báo nhập điểm sai. Vui lòng liên hệ lập trình viên để xem lại");
        }
        CODE_RESULT_RETURN UpdateBangDiem(TRANGTHAI_BANGDIEM trangThai)
        {
            var dsHocVien = bdHocVien.DataSource as List<HocVien>;
            var lopHP = bdLop.Current as DotThi_LopHocPhan;
            var dotThi = cbxDotThi.SelectedItem as DotThi;
            if (dsHocVien != null && dsHocVien.Count > 0 && lopHP != null && dotThi != null)
                return BizLopHocPhan_BangDiem.UpdateBangDiem(lopHP.MaLopHocPhanGUID, dotThi.MaDotThiGUID, trangThai, FormMain.NguoiSuDung.UserName);
            else
                return CODE_RESULT_RETURN.ThatBai;
        }
    }
}
